package cn.xz.hfsk.util

import android.content.Context
import android.content.SharedPreferences
import cn.xz.hfsk.base.BaseApplication
import cn.xz.hfsk.bean.CommonLib
import com.google.gson.Gson
import com.google.gson.JsonParser
import java.lang.reflect.Type

/**
 * 本地存储
 * */
object SessionUtils {

    private var preferences: SharedPreferences? = null

    private val sharePreferences: SharedPreferences
        get() {
            if (preferences == null) {
                synchronized(SharedPreferences::class.java) {
                    if (preferences == null) {
                        preferences = BaseApplication.getContext().getSharedPreferences(
                            CommonLib.JC_SP_KEY,
                            Context.MODE_PRIVATE
                        )
                    }
                }
            }
            return preferences!!
        }

    private val editor: SharedPreferences.Editor
        get() = sharePreferences.edit()

    fun clearAll(){
        editor.clear()
        editor.commit()
    }

    fun getString(key: String, value: String): String {
        return sharePreferences.getString(key, value) ?: value
    }

    fun getBoolean(key: String, value: Boolean): Boolean {
        return sharePreferences.getBoolean(key, value)
    }

    fun getInt(key: String, value: Int): Int {
        return sharePreferences.getInt(key, value)
    }

    fun getLong(key: String, value: Long): Long {
        return sharePreferences.getLong(key, value)
    }

    fun getFloat(key: String, value: Float): Float {
        return sharePreferences.getFloat(key, value)
    }

    fun getStringSet(key: String, value: Set<String>): Set<String>? {
        return sharePreferences.getStringSet(key, value)
    }

    fun commitString(key: String, value: String) {
        val editor = editor.putString(key, value)
        commit(editor)
    }

    fun commitInt(key: String, value: Int) {
        val editor = editor.putInt(key, value)
        commit(editor)
    }

    fun commitFloat(key: String, value: Float) {
        val editor = editor.putFloat(key, value)
        commit(editor)
    }

    fun commitLong(key: String, value: Long) {
        val editor = editor.putLong(key, value)
        commit(editor)
    }

    fun commitBoolean(key: String, value: Boolean) {
        val editor = editor.putBoolean(key, value)
        commit(editor)
    }

    fun commitStringSet(key: String, value: Set<String>) {
        val editor = editor.putStringSet(key, value)
        commit(editor)
    }

    fun remove(key: String) {
        editor.remove(key).commit()
    }

    private fun commit(editor: SharedPreferences.Editor) {
        editor.commit()
    }

    fun commitObject(key: String, source: Any) {
        val editor = editor.putString(key, JsonUtil.serializeAsString(source))
        commit(editor)
    }

    fun <T> getObject(key: String, klass: Type): T? {
        val string: String = getString(key, "")
        if (string == "" || string == "\"\"") return null
        return if (string == "") {
            null
        } else {
            JsonUtil.deserializeAsObject(string, klass)
        }
    }


    //存储集合数据
    fun <T> commitDataList(key: String,dataList: List<T>?) {
        if (null == dataList || dataList.size < 0) {
            return
        }
        val gson = Gson()
        val strJson = gson.toJson(dataList)
        val editor = sharePreferences.edit()
        editor.putString(key, strJson)
        editor.commit()
    }

    //获取集合数据
    fun <T> getDataList(key: String,cls: Class<T>?): MutableList<T> {
        val dataList: MutableList<T> = ArrayList()
        val strJson = sharePreferences.getString(key, null) ?: return dataList
        val gson = Gson()
        val myList = JsonParser().parse(strJson).asJsonArray
        for (jsonElement in myList) {
            dataList.add(gson.fromJson(jsonElement, cls))
        }
        return dataList
    }

}